package cn.sdormitory.controller.workflow;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.workflow.entity.NotRetrunedApply;
import cn.sdormitory.workflow.service.NotRetrunedApplyService;
import cn.sdormitory.workflow.vo.NotRetrunedApplyVO;
import cn.sdormitory.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 宿舍未归工作流
 */
@RestController
@RequestMapping("/notReturned")
@Slf4j
public class NotReturnedApplyController {

    @Resource
    NotRetrunedApplyService notRetrunedApplyService;
    /**
     * 提交流程
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/createApply")
    public CommonResult<Integer> createApply(@RequestBody NotRetrunedApplyVO vo){
        int apply = notRetrunedApplyService.createApply(vo);
        if(apply>0){
            return CommonResult.success();
        }
        if(apply==-1){
            return CommonResult.failed("学生ID为空");
        }
        if(apply==-2){
            return CommonResult.failed("学生当天已经提交过处理申请");
        }
        return CommonResult.failed();
    }

    /**
     * 查询申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/pageList")
    public CommonResult<CommonPage<NotRetrunedApplyVO>> pageList(@RequestBody NotRetrunedApplyVO vo){
        IPage<NotRetrunedApplyVO> notRetrunedApplyVOIPage = notRetrunedApplyService.pageList(vo);
        return CommonResult.success(CommonPage.restPage(notRetrunedApplyVOIPage));
    }

    /**
     * 撤销申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/cancelApply")
    public CommonResult<CommonPage<NotRetrunedApplyVO>> cancelApply(@RequestBody NotRetrunedApplyVO vo){
        boolean b = notRetrunedApplyService.cancelApply(vo);
        if(b){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询申请单详情
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/detail")
    public CommonResult<NotRetrunedApplyVO> applyDetail(@RequestBody NotRetrunedApplyVO vo){
        return CommonResult.success(notRetrunedApplyService.applyDetail(vo));
    }

    /**
     * 已审批申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/findHistoryApproval")
    public CommonResult<CommonPage<WorkFlowVO>> findHistoryApproval(@RequestBody WorkFlowVO vo) throws ApiException{
        IPage<WorkFlowVO> list = notRetrunedApplyService.findHistoryApproval(vo);
        return CommonResult.success(CommonPage.restPage(list));
    }
}
